package com.yc.javaee.servlet.d0613;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

/**
 * http://localhost/C122_S2_Ply_JavaEE_war/0613/forward.s  地址错误
 * http://localhost/C122_S2_Ply_JavaEE_war/forward.s
 */
@WebServlet("/forward.s")
public class ForwardServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 数组参数
        String[] likes = req.getParameterValues("like");
        System.out.println(Arrays.toString(likes));
        List<String> list = Arrays.asList(likes);
        if( list.contains("请求转发")){
            // 获取转发器
            RequestDispatcher rd = req.getRequestDispatcher("add.s?a=344&b=433");
            // 执行转发
            rd.forward(req,resp);
        } else if( list.contains("响应重定向")){
            // http://localhost/C122_S2_Ply_JavaEE_war/request.html
            resp.sendRedirect("0613/request.html");
        } else {
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.append("<select>");
            for (String s : list) {
                out.append("<option>").append(s).append("</option>");
            }
            out.append("</select>");
        }
    }
}
